package fr.imag.ufrima.ctte.gui.dialogs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JTextField;

import fr.imag.ufrima.ctte.ImagCTTE;

public class ChangeTextDialog extends JDialog {
	private ImagCTTE imagCTTE;
	private final JTextField text;
	
	/**
	 * Constructor
	 */
	public ChangeTextDialog(ImagCTTE imagCTTE, final JTextField text){
		this.imagCTTE = imagCTTE;
		this.text = text;
		InitComponents();
		txtText.setText(text.getText());
		this.setLocationRelativeTo(null);
		this.setAlwaysOnTop(true);
		this.setTitle("Change text");
		this.setModal(true);
		
		btnOk.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				
				String txt = txtText.getText();
        		txt.trim();
        		if (txt.equals("") != true){
        			getImagCTTE().getCurrentTaskDiagram().getSelectedTask().setDescription(txt);        			
        			text.setText(txt);
        		}
        		
				ChangeTextDialog.this.dispose();
			}
		});
		btnCancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {				
				ChangeTextDialog.this.dispose();
			}
		});
	}
	/**
	 * Initialize componenets
	 */
	protected void InitComponents(){

		btnOk = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtText = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Platforms");

        btnOk.setText("Ok");

        btnCancel.setText("Cancel");

        txtText.setColumns(20);
        txtText.setRows(5);
        jScrollPane1.setViewportView(txtText);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 318, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnCancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnOk)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOk)
                    .addComponent(btnCancel))
                .addContainerGap())
        );

        pack();
	}
	
	/**
	 * Components
	 */
	private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOk;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtText;

	public ImagCTTE getImagCTTE() {
		return imagCTTE;
	}
	public void setImagCTTE(ImagCTTE imagCTTE) {
		this.imagCTTE = imagCTTE;
	}
}
